//
//  DataDBManager.m
//  Romens.XSBalance
//
//  Created by 孙博 on 16/3/16.
//  Copyright © 2016年 孙博. All rights reserved.
//

#import "DataDBManager.h"
#import "userInformation.h"
#import "usertestInformation.h"
#import "DBUntilManager.h"
#import "ToolHelper.h"
#define UserTestInfo [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS UserTestInfo (GUID CHAR(50) PRIMARY KEY,USERID  CHAR(100),HEALTHTYPE CHAR(50),KEY CHAR(50),VALUE CHAR(50),UNIT CHAR(50),CREATED CHAR(50),UPDATED CHAR(50),CREATETIME CHAR(50))"]

@interface DataDBManager ()

@end

@implementation DataDBManager

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(instancetype)init
{
    self=[super init];
    if(self)
    {
        [self CreateTable];
    }
    return  self;
}

-(void)CreateTable
{
    [[DBUntilManager instance] createTable:UserTestInfo];
}

-(void)ClearTable
{
    [[DBUntilManager instance]  clearTable:@"UserTestInfo"];
}

-(NSString*)GetCurrentDate
{
    
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970] ;
    NSString *timeString = [NSString stringWithFormat:@"%d", (long)interval];
    return timeString;
}
/**
 *  将测量数据插入本地
 *
 *  @param userinf    <#userinf description#>
 *  @param suessblock <#suessblock description#>
 */
-(void)InsertUserTestData:(usertestInformation*)userinf suessblock:(void (^)(id))suessblock
{
    NSMutableArray *sqlalls=[[NSMutableArray alloc] init];
    /**
     *   体重
     */
    NSMutableArray *sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"WEIGHT"];
    [sql addObject:isEmptyString(userinf.Weight)?@"0":userinf.Weight];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
     [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
;
    /**
     *   BMI
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"BMI"];
    [sql addObject:isEmptyString(userinf.BMI)?@"0":userinf.BMI];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
    /**
     *  脂肪率
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"FAT"];
    [sql addObject:isEmptyString(userinf.Fat)?@"0":userinf.Fat];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
    /**
     *骨骼值
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"BONES"];
    [sql addObject:isEmptyString(userinf.Bones)?@"0":userinf.Bones];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
    /**
     *水分值
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"WATER"];
    [sql addObject:isEmptyString(userinf.Water)?@"0":userinf.Water];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];

    /**
     *肌肉值
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"MUSCLE"];
    [sql addObject:isEmptyString(userinf.Muscle)?@"0":userinf.Muscle];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
    /**
     *   卡路里
     */
    sql=[NSMutableArray new];
    [sql addObject:[ToolHelper NewGuid]];
    [sql addObject:userinf.USERID];
    [sql addObject:userinf.HEALTHTYPE];
    [sql addObject:@"CALORIE"];
    [sql addObject:isEmptyString(userinf.Calorie)?@"0":userinf.Calorie];
    [sql addObject:userinf.TestUnit];
    [sql addObject:userinf.CreateDate];
    [sql addObject:[self GetCurrentDate]];
    [sql addObject:userinf.CreateTime];
    [sqlalls addObject:sql];
    
    
     NSString* sqlstr=[NSString stringWithFormat: @"insert into %@(GUID,USERID,HEALTHTYPE,KEY,VALUE,UNIT,CREATED,UPDATED,CREATETIME) values(?,?,?,?,?,?,?,?,?)",@"UserTestInfo"];
    [[DBUntilManager instance] ExecSql:sqlstr array:sqlalls  completion:^(NSString *messages) {
        suessblock(messages);
    }];

}

/**
 *  查询测试数据
 *
 *  @param startDate  <#startDate description#>
 *  @param endDate    <#endDate description#>
 *  @param suessblock <#suessblock description#>
 */
-(void)GetUserTestData:(NSString*)USERID startdate:(NSString*)startDate endDate:(NSString*)endDate key:(NSString*)key suessblock:(void (^)(id))suessblock
{
   NSString*selectSql=[NSString stringWithFormat: @"select  AVG(VALUE) VALUE,CREATED from UserTestInfo where CREATED between ('%@') and ('%@') and USERID='%@' and key='%@' group by  CREATED",startDate,endDate,USERID,key];
    //获取最大Update
    [[DBUntilManager instance] GetData:selectSql completion:^(NSArray *messages, NSError *error) {
        suessblock(messages);
    }];

}

@end
